package classes.pack;

import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.app.Service;
import android.content.Intent;
import android.location.GpsStatus;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;

public class Gps extends GeoLoc implements LocationListener
{
	private static final int TWO_MINUTES = 1000 * 60 * 2;
	private static final int ONE_MINUTE = 1000 * 60;
	private String provider;
	private LocationManager locationManager;
	private Criteria criteria;	
	public GeoPoint LocalGeo = new GeoPoint(0, 0);
	public Context con;
	
	public GeoPoint loc2geo(Location loc)
    {
    	GeoPoint Gp = new GeoPoint((int)(loc.getLatitude() * 1E6), (int)(loc.getLongitude()* 1E6)); 
    	return Gp;
    }
	
	public void StartGps()
	{
		
		try
		{
			
			locationManager = (LocationManager) con.getSystemService(Context.LOCATION_SERVICE);
			this.criteria = new Criteria();
			this.provider = locationManager.getBestProvider(criteria, false);
			locationManager.requestLocationUpdates(this.provider, ONE_MINUTE, 0, locationListener);
		}
		catch(Exception e)
		{
			Toast toast = Toast.makeText(con, "Starting Gps failed", Toast.LENGTH_LONG);
			toast.show();
		}
	}
	
	public void StopGps()
	{
		locationManager.removeUpdates(locationListener);
	}
	
	public Gps(GeoLocType geoLocType, Context context) 
	{
		super(geoLocType);
		con = context;		
	}
	
	LocationListener locationListener = new LocationListener() 
	{
	    public void onLocationChanged(Location location) 
	    {
	    	LocalGeo = loc2geo(location);
	    	Logging.updatePosition(LocalGeo);
	    	
	    	Toast toast = Toast.makeText(con, "gpslistener called", Toast.LENGTH_LONG);
			toast.show();
	    }

	    public void onStatusChanged(String provider, int status, Bundle extras) 
	    {}

	    public void onProviderEnabled(String provider) 
	    {}

	    public void onProviderDisabled(String provider) 
	    {}
	  };

	
	public GeoPoint getCurrentPosition()
	{
		return LocalGeo;
	}

	public void onLocationChanged(Location arg0) 
	{		
	}

	public void onProviderDisabled(String arg0) 
	{		
	}

	public void onProviderEnabled(String arg0) 
	{			
	}

	public void onStatusChanged(String arg0, int arg1, Bundle arg2) 
	{		
	}
	
}
